// TinyMsgInterface.cpp : Implementation of CTinyMsgInterface

#include "stdafx.h"
#include "TinyMsgInterface.h"
#include <comdef.h>


// CTinyMsgInterface


STDMETHODIMP CTinyMsgInterface::connect(BSTR IP, LONG port, VARIANT_BOOL* result)
{
	// TODO: Add your implementation code here
	*result = FALSE;
	bool ret = m_client.connectToServer( _bstr_t(IP),port, m_hRecHandle);

	if(ret == true)
		*result = TRUE;
	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::send(VARIANT_BOOL* result)
{
	// TODO: Add your implementation code her
	*result = FALSE;
	//reconnect
	//processTimer();

	TinyCC::msg::CTinyTCPMsg tcpmsg;

	long length=0;
	const char * data =tcpmsg.package(length,&m_tinyMsg);

	//send msg in double server env

	if(m_client.getIsConnected())
	{
		if(m_client.sendData((void*)data,length))
			*result = TRUE;
		else
			*result = FALSE;
	}
	else
	{
		*result = FALSE;
	}
	//clear msgs
	m_tinyMsg.clear();


	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::nextMsg(VARIANT_BOOL* result)
{
	// TODO: Add your implementation code here
	*result = FALSE;

	TinyCC::msg::CTinyTCPRawMsg* rawMsg = NULL;
	TinyCC::msg::CTinyTCPMsg tcpmsg;

	if(m_client.getIsConnected() == true)
	{
		rawMsg = m_client.getNextMsg();

		if( rawMsg != NULL )
		{
			//not finished
			m_tinyMsg.clear();
			if(tcpmsg.unPackage(*rawMsg,&m_tinyMsg))
				*result = TRUE;
			else
			{
				m_tinyMsg.clear();
				*result = FALSE;
			}
		}
	}

	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::clear(void)
{
	// TODO: Add your implementation code here
	m_tinyMsg.clear();
	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::setMsgIndex(LONG index)
{
	// TODO: Add your implementation code here
	m_tinyMsg.setMsgIndex((WORD)index);
	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::setMsgItem(LONG item)
{
	// TODO: Add your implementation code here
	m_tinyMsg.setMsgItem((WORD)item);
	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::talk(VARIANT_BOOL* result)
{
	*result = FALSE;
	send(result);
	if(*result != FALSE)
	{
		*result = FALSE;
		for(int i =0;i<100;++i)
		{
			Sleep(100);
			nextMsg(result);
			if(*result == TRUE)
				break;
		}
	}
	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::getMsgIndex(LONG* value)
{
	// TODO: Add your implementation code here

	*value = m_tinyMsg.getMsgIndex();
	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::getMsgItem(LONG* value)
{
	// TODO: Add your implementation code here
	*value = m_tinyMsg.getMsgItem();
	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::getDefaultParamByIndex(LONG index, BSTR* value)
{
	// TODO: Add your implementation code here
	std::string valuestr;
	m_tinyMsg.getDefaultParamByIndex(index,valuestr);
	_bstr_t bstrValue =  valuestr.c_str();
	*value = bstrValue.copy();
	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::getDefaultParamCount(LONG* value)
{
	// TODO: Add your implementation code here

	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::setDefaultParamByIndex(LONG index, BSTR value, VARIANT_BOOL* ret)
{
	// TODO: Add your implementation code here

	if(index<100)
		* ret = TRUE;/*limit dp size */
	
	int count = index - m_tinyMsg.getDefaultParamCount();
	if(count > 0)
	{
		for(int i = 1;i<count;++i)
			m_tinyMsg.pushBackDefaultParam( _bstr_t("") );
		m_tinyMsg.pushBackDefaultParam( _bstr_t(value) );
	}
	else
	{
		m_tinyMsg.setDefaultParam(index, _bstr_t(value) );
	}
	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::shutdown(BSTR* succ)
{
	// TODO: Add your implementation code here

	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::getExternalParamKeyCount(LONG index, LONG* count)
{
	// TODO: Add your implementation code here
	*count = m_tinyMsg.getExternalParamKeyCountByIndex(index);
	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::getExternalParamValueCount(BSTR key, LONG* count)
{
	// TODO: Add your implementation code here


	return S_OK;
}

STDMETHODIMP CTinyMsgInterface::getExternalParamValue(LONG keyindex, BSTR key, LONG valueindex, BSTR* value)
{
	// TODO: Add your implementation code here

	return S_OK;
}
